package com.example.lock.zkLock;

import org.I0Itec.zkclient.serialize.BytesPushThroughSerializer;

import java.util.concurrent.TimeUnit;

public class TestDistributedLock {

    public static void main(String[] args) {

        final ZkClientExt zkClientExt1 = new ZkClientExt("47.97.173.74:9555", 5000, 5000, new BytesPushThroughSerializer());
        final SimpleZookeeperLock mutex1 = new SimpleZookeeperLock(zkClientExt1, "/Mutex");

        final ZkClientExt zkClientExt2 = new ZkClientExt("47.97.173.74:9555", 5000, 5000, new BytesPushThroughSerializer());
        final SimpleZookeeperLock mutex2 = new SimpleZookeeperLock(zkClientExt2, "/Mutex");

        try {
            mutex1.acquireLock("12", 12L, 12L);
            System.out.println("Client1 locked");
            Thread client2Thd = new Thread(new Runnable() {
                public void run() {
                    try {
                        mutex2.acquireLock("12", 12L, 12L);
                        System.out.println("Client2 locked");
                        mutex2.release("212","232323");
                        System.out.println("Client2 released lock");

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            client2Thd.start();
            Thread.sleep(5000);
            mutex1.release("212","232323");
            System.out.println("Client1 released lock");

            client2Thd.join();

        } catch (Exception e) {

            e.printStackTrace();
        }

    }

}
